package com.sanpu.kxe.business.member.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.member.adapter.MemberSpreadAdapter;
import com.sanpu.kxe.business.member.bean.MemberSpreadOutput;
import com.sanpu.kxe.business.member.contract.MemberSpreadContract;
import com.sanpu.kxe.business.member.presenter.MemberSpreadPresenter;
import com.sanpu.kxe.business.member.provider.MemberOpenProvider;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;

/**
 * Description：会员推广
 * Created by 薛勇军 on 2019/4/12.
 */

public class MemberSpreadActivity extends BaseTitleActivity implements MemberSpreadContract.View {

    @Bind(R.id.list_view)
    RecyclerView vListView;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;

    private MemberSpreadAdapter mSpreadAdapter;

    private MemberSpreadContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_member_spread;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("我的推广");
        vListView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mSpreadAdapter = new MemberSpreadAdapter();
        mSpreadAdapter.setEnableLoadMore(false);
        vListView.setAdapter(mSpreadAdapter);

        mPresenter = new MemberSpreadPresenter(this);
        mPresenter.getMemberSpreadList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        mSpreadAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position >= adapter.getData().size()) return;
                MemberSpreadOutput spreadOutput = (MemberSpreadOutput)adapter.getItem(position);
                if (spreadOutput != null) {
                    //用户列表
                    MemberOpenProvider.getInstance()
                            .openMemberPromotListActivity(mContext, spreadOutput.getMerchantLevel());
                }
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getMemberSpreadList();
            }
        });
    }

    @Override
    public void showMemberSpreadView(List<MemberSpreadOutput> memberSpreadOutputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (memberSpreadOutputs != null && memberSpreadOutputs.size()>0) {
            mSpreadAdapter.setNewData(memberSpreadOutputs);
            mSpreadAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MemberSpreadContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
